//
//  BuyOrSellTableViewCell.swift
//  Exchange
//
//  Created by rui on 2018/4/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
import SDWebImage

class BuyOrSellTableViewCell: UITableViewCell {

    @IBOutlet weak var icon: UIImageView!
    
    @IBOutlet weak var name: UILabel!
    ///支付方式
    @IBOutlet weak var payType: UIImageView!
    
    @IBOutlet weak var buyOrSell: UIButton!
    
    @IBOutlet weak var price: UILabel!
    ///好评
    @IBOutlet weak var favourableComment: UILabel!
    ///信任数量
    @IBOutlet weak var trustAccount: UILabel!
    ///交易数量
    @IBOutlet weak var exchangeAccount: UILabel!
    
    @IBOutlet weak var limitPrice: UILabel!
    
    var model: AdvertisingListModel?
    
    typealias IconClick = (_ userId: Int)->()
    
    var clickIcon: IconClick?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.buyOrSell.layer.cornerRadius = 3
        self.buyOrSell.layer.borderWidth = 0.5
        self.buyOrSell.layer.borderColor = kThemeColor.cgColor
        let tap = UITapGestureRecognizer(target: self, action: #selector(iconClick))
        icon.isUserInteractionEnabled = true
        icon.addGestureRecognizer(tap)
        icon.layer.cornerRadius = 22.5
        icon.layer.masksToBounds = true
        price.adjustsFontSizeToFitWidth = true
        self.themeBackground = ThemeManager.ThemeColorKeys.background
        let view = UIView(frame: self.bounds)
        view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        self.selectedBackgroundView = UIView(frame: self.bounds)
        self.selectedBackgroundView?.themeBackground = ThemeManager.ThemeColorKeys.selectTabColor
    }
    
    func configerModel(model: AdvertisingListModel) {
        self.model = model
        
        if let portrait = model.portrait, let url = URL(string: portrait) {
            self.icon.sd_setImage(with: url, placeholderImage:#imageLiteral(resourceName: "login_icon") )
        }else {
            self.icon.image = #imageLiteral(resourceName: "login_icon")
        }
        if let name = model.userName, name.count > 0 {
            self.name.text = name
        }else {
            self.name.text = "--"
        }
        var title = kText("出售1")
        if model.type == 1 {
            title = kText("购买1")
        }
        
        let moneyType = model.moneyType ?? ""
        self.buyOrSell.setTitle(title, for: .normal)
        self.exchangeAccount.text = String(format: "%ld", model.dealCount ?? 0)
        self.favourableComment.text = model.highOpinionRatio
        self.trustAccount.text = String(format: "%ld", model.trustNum ?? 0)
        let currency = model.currency ?? ""
        self.price.text = "0.00 \(currency)"
        if let exTick = AppContext.default.exTickData {
            let datas = exTick.filter { (data) -> Bool in
                return data["money"] as? String == currency
            }
            if let data = datas.first,let symbols = data["symbols"] as? [String:Double]  {
                if var exPrice = symbols[moneyType] {
                    exPrice = exPrice + exPrice * (model.premium ?? 0) / 100
                    self.price.text = String(format: "%0.2lf %@", exPrice,currency)
                }
            }
        }
        let payType = model.paymentMethod ?? 1
        if payType == 2 {
            self.payType.image = #imageLiteral(resourceName: "unionPay")
        }else {
            self.payType.image = #imageLiteral(resourceName: "aliPay")
        }
        
        let lowest = model.limitLowerMoney ?? 0
        let high = model.limitUpperMoney ?? 0
        self.limitPrice.text = String(format: "%0.2lf~%0.2lf %@", lowest,high,currency)
        
        if let userId = model.userId,let myid = AppInfo.default.userId,userId == myid {
            self.buyOrSell.setTitle(kText("编辑广告"), for: .normal)
        }
    }
    
    @objc func iconClick(){
        if let userId = self.model?.userId {
            self.clickIcon?(userId)
        }else {
            CYMBShow.showMessage("用户不存在")
        }
    }
    
}
